package com.blog.controller;
import com.blog.Utils.Result;
import com.blog.annotion.LoginAnnotatio;
import com.blog.service.DataService;
import com.blog.bean.vo.DataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author 任浩
 * @version 1.0
 */

@RestController
@RequestMapping("/data")
@Slf4j
public class DataController {

    @Autowired
    private DataService dataService;

    /**
     * 一开始加载当前页面的时候，获取当天的访问量。
     * @return
     */
    @GetMapping
    @LoginAnnotatio
    public Result getCur(){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = LocalDateTime.now().format(dateTimeFormatter);
        LocalDateTime localDateTime = LocalDate.parse(format,dateTimeFormatter).atStartOfDay();
        Long uv = dataService.getUV(localDateTime, localDateTime);
        Long dau = dataService.getDAU(localDateTime, localDateTime);
        DataVO dataVO = new DataVO();
        dataVO.setDataUV(uv);
        dataVO.setDataDAU(dau);
        return Result.success(dataVO);
    }


    /**
     * 得到当前网站的独立访问用户
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/uv")
    @LoginAnnotatio
    public Result getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") String  start,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd")String end){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime localDateTimeStart = LocalDate.parse(start,dateTimeFormatter).atStartOfDay();
        localDateTimeStart.plusDays(1);
        LocalDateTime localDateTimeEnd = LocalDate.parse(end,dateTimeFormatter).atStartOfDay();
        System.out.println("当前时间为"+ localDateTimeStart+ " " + localDateTimeEnd);
        Long count = dataService.getUV(localDateTimeStart,localDateTimeEnd);
        return Result.success(count);
    }

    /**
     * 得到当前网站的日访问用户
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/dau")
    @LoginAnnotatio
    public Result getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") String  start,
                         @DateTimeFormat(pattern = "yyyy-MM-dd")String end){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime localDateTimeStart = LocalDate.parse(start,dateTimeFormatter).atStartOfDay();
        localDateTimeStart.plusDays(1);
        LocalDateTime localDateTimeEnd = LocalDate.parse(end,dateTimeFormatter).atStartOfDay();
        Long count = dataService.getDAU(localDateTimeStart,localDateTimeEnd);

        return Result.success(count);
    }
}
